//
//  XDMeViewController.m
//  XDEducation
//
//  Created by apple on 2017/4/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDMeViewController.h"
#import "XDMeHeaderView.h"
#import "XDMeCellData.h"
#import "XDLoginViewController.h"
#import "XDMeSettingController.h"
#import "XDUpdatePwdView.h"
#import "XDExchangeViewController.h"
#import "WebViewController.h"
#import "XDMiyoshiBankViewController.h"
//#import "XDMiyoshiBankViewController.h"
@interface XDMeViewController ()

@property (nonatomic, strong) XDMeHeaderView *headerView;

/** cell数据 */
@property(nonatomic,strong) NSMutableArray *cellData;

/** 修改密码 */
@property(nonatomic,strong) XDUpdatePwdView *updatePwdView;
@end

#define headerBtnHeight 60 * ScaleHeight
#define headerViewHeight (200 + 60) * ScaleHeight

@implementation XDMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.tableView.frame = CGRectMake(0 , -64, ScreenW, ScreenH + 64);
    self.navigationController.view.backgroundColor = ThemeColor;
    //设置navigationBar透明
    [self setNavigationBar];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //设置navigationBar透明
    //[self setNavigationBar];
    //self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.clipsToBounds = YES;
    self.navigationController.navigationBar.translucent = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.clipsToBounds = NO;
    self.navigationController.navigationBar.translucent = NO;
}

/**
 设置navigationBar透明
 */
-(void)setNavigationBar{
    self.navigationItem.title = @"";
    
    XDMeHeaderView *tableHeaderView = [[XDMeHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, headerViewHeight)];
    WeakSelf(weakself)
    tableHeaderView.iconClickedBlock = ^(){     //头像点击事件
        [weakself setting];
    };
    tableHeaderView.functionClickedBlock = ^(NSString *functionName)
    {
        //三好富豪榜 三好银行 兑换 星星礼包 点击事件
        [weakself functionController:functionName];
    };
    self.tableView.tableHeaderView = tableHeaderView;
    self.headerView = tableHeaderView;
    
    //图层渲染 设置导航栏透明
    self.navigationController.navigationBar.translucent = YES;
    UIColor *color = ThemeColor;
    CGRect rect = CGRectMake(0, -64, ScreenW, 64);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.clipsToBounds = YES;
    
    
    UIButton *navRightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 25)];
    [navRightBtn setImage:[UIImage imageNamed:@"mine_icon_setting"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:navRightBtn];
    [navRightBtn addTarget:self action:@selector(rightBarButtonItemClicked) forControlEvents:UIControlEventTouchUpInside];
}


-(NSMutableArray *)cellData{
    
    if (!_cellData) {
        
        _cellData = [NSMutableArray array];
        _cellData = [XDMeCellData getMeCellData];
    }
    return _cellData;
}


#pragma mark - tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.cellData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50 * ScaleHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"meCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    NSString *cellName = self.cellData[indexPath.row][@"name"];
    NSString *cellIcon = self.cellData[indexPath.row][@"icon"];
    
    cell.imageView.image = [UIImage imageNamed:cellIcon];
    cell.textLabel.text = cellName;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellName = self.cellData[indexPath.row][@"name"];
    
    if([cellName isEqualToString:@"设置支付密码"]){   //弹窗  设置支付密码
        
        _updatePwdView = [[XDUpdatePwdView alloc]initWithFrame:CGRectMake(0, 0 , ScreenW - 40, 300)];
        _updatePwdView.center =  CGPointMake(self.view.center.x, self.view.center.y - 64);
        //WeakSelf(weakself)
        _updatePwdView.sureUpdatePwdBlock = ^(NSString *_oldpwd ,NSString *_newpwd ,NSString *_newpwdAgain){
            
            if (_oldpwd.length < 6 || _newpwdAgain.length < 6 || _newpwd.length < 6) {
                [MBProgressHUD showError:@"密码格式错误"];
                return ;
            }
            if(![_newpwd isEqualToString:_newpwdAgain]){
                [MBProgressHUD showError:@"两次输入的密码不一致"];
                return;
            }
#warning 未完成 缺网络请求
//            [weakself updatePwdNetWorkOldpwd:_oldpwd newpwd:_newpwd];
        };
        
        [_updatePwdView show];
        
    }else if ([cellName isEqualToString:@"三好指南"])
    {
        WebViewController *VC = [[WebViewController alloc] init];
        VC.str = BaseURL(@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_SHSMS&pageNumber=1");
        VC.nameTitle = @"三好指南";
        [self.navigationController pushViewController:VC animated:YES];
    }
    else
    {
        
        Class c = NSClassFromString(self.cellData[indexPath.row][@"vc"]);
        UIViewController *vc = [[c alloc]init];
        vc.title = cellName;
        [self.navigationController pushViewController:vc animated:YES];
        NSLog(@"%@",cellName);
    }
    
}

-(void)rightBarButtonItemClicked{
    
    [self setting];
}


-(void)setting{
    
    if ([AccountTool shareAccountTool].currentAccount.username.length == 0) {
        
        XDLoginViewController *vc = [[XDLoginViewController alloc]init];
        [self presentViewController:vc animated:YES completion:nil];
    }else{
        XDMeSettingController *vc = [[XDMeSettingController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//模块跳转 三好富豪榜 三好银行 兑换 星星礼包
- (void)functionController:(NSString *)functionName
{
    NSArray *array = @[
                       @{@"name":@"三好富豪榜" ,@"vc":@""},
                       @{@"name":@"三好银行"   ,@"vc":@"XDMiyoshiBankViewController"},
                       @{@"name":@"兑换"      ,@"vc":@"XDExchangeViewController"},
                       @{@"name":@"星星礼包"   ,@"vc":@""},
                       ];
    
    if ([functionName isEqualToString:@"星星礼包"])
    {
        WebViewController *WebVC = [[WebViewController alloc] init];
        NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"wcbfws.do?action=showStartGift&forumcode=XXDD_XXSQ_XXDLB&pageNumber=1"];
        WebVC.str = str;
        WebVC.nameTitle = @"星星礼包";
        [self.navigationController pushViewController:WebVC animated:YES];
    }
    else if ([functionName isEqualToString:@"三好富豪榜"])
    {
        WebViewController *WebVC = [[WebViewController alloc] init];
        WebVC.str = BaseURL(@"bbsws.do?action=showalltopic&forumcode=XXDD_XXSQ_GRJZ_WSXW&pageNumber=1");
        WebVC.nameTitle = @"三好富豪榜";
        [self.navigationController pushViewController:WebVC animated:YES];
    }
    else if ([functionName isEqualToString:@"三好银行"])
    {
        XDMiyoshiBankViewController *VC = [[XDMiyoshiBankViewController alloc] init];
        VC.title = @"三好银行";
        [self.navigationController pushViewController:VC animated:YES];
    }
    else
    {
        XDExchangeViewController *VC = [[XDExchangeViewController alloc] init];
        VC.title = @"兑换";
        [self.navigationController pushViewController:VC animated:YES];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint offset = scrollView.contentOffset;
    
    if (offset.y < -64) {
        
        CGFloat addHeight = -( offset.y + 64);
        
        self.tableView.tableHeaderView.frame = CGRectMake(0, 0, ScreenW, headerViewHeight + addHeight);
        
        self.headerView.bgImageView.frame = CGRectMake(0, -addHeight, ScreenW, headerViewHeight + addHeight - headerBtnHeight + 5);
    }
    
}

@end
